package jgl.vajra.driver.widget.recyclerView.pagemanagestrategy;

import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;

import jgl.vajra.driver.R;
import jgl.vajra.driver.constant.Constant;
import jgl.vajra.driver.databinding.FootRefreshRecyclerviewBinding;
import jgl.vajra.driver.widget.bean.FootRefreshBean;
import jgl.vajra.driver.widget.databindingadapter.BaseDataBindingDecorator;
import jgl.vajra.driver.widget.utils.DisplayUtils;

import androidx.databinding.ViewDataBinding;
import io.reactivex.disposables.Disposable;

/**
 * @Author zhangshuqi
 * @CreateTime 2018/3/19
 * @Describe
 */

public class RecyclerFootPageManageStrategy extends LoadingManageStrategy {

    private FootRefreshBean footLoadMoreBean;

    public RecyclerFootPageManageStrategy(PageManageBuilder builder) {
        super(builder);
    }


    @Override
    public void showPageContent() {
        footLoadMoreBean.setStatus(4);
        // mBuilder.getAdapter().refresh(mBuilder.getAdapter().getListData().size());
        mBuilder.getAdapter().notifyDataSetChanged();
    }

    @Override
    public void showPageLoading(String loadingMsg, Disposable d) {
        addFootRefreshView();
    }

    @Override
    public void showPageEmpty(String emptyMsg) {
      /*  HashMap<Integer, BindingViewHolder> bindingViewHolderMap = mBuilder.getAdapter().getBindingViewHolderMap();
        BindingViewHolder bindingViewHolder = bindingViewHolderMap.get(Constant.ADAPTER_FOOT_KEY.REFRESH_RECYCLER_FOOT_KEY);
        if (bindingViewHolder != null) {
            FootRefreshRecyclerviewBinding binding = (FootRefreshRecyclerviewBinding) bindingViewHolder.getBinding();
            onHandlerEmptyView(emptyMsg, binding);

        }*/
        footLoadMoreBean.setStatus(1);
        //  mBuilder.getAdapter().refresh(mBuilder.getAdapter().getListData().size());
        mBuilder.getAdapter().notifyDataSetChanged();

    }

    private void onHandlerEmptyView(String emptyMsg, FootRefreshRecyclerviewBinding binding) {
        if (mBuilder.getEmptyImageRes() != 0) {
            binding.pageEmpty.ivImage.setImageResource(mBuilder.getEmptyImageRes());
        }
        if (!TextUtils.isEmpty(emptyMsg))
            binding.pageEmpty.tvEmptyMsg.setText(emptyMsg);
    }

    @Override
    public void showPageError(String errorMsg) {
      /*  if (!PageManager.isNetWorkAvailable(mBuilder.getContext()) || mBuilder.getErrorImageRes() != 0) {
            HashMap<Integer, BindingViewHolder> bindingViewHolderMap = mBuilder.getAdapter().getBindingViewHolderMap();
            BindingViewHolder bindingViewHolder = bindingViewHolderMap.get(Constant.ADAPTER_FOOT_KEY.REFRESH_RECYCLER_FOOT_KEY);
            if (bindingViewHolder != null) {
                FootRefreshRecyclerviewBinding binding = (FootRefreshRecyclerviewBinding) bindingViewHolder.getBinding();
                onHandlerErrorView(binding);
            }
        }
*/
        footLoadMoreBean.setStatus(2);
        // 添加重试操作
        //  mBuilder.getAdapter().refresh(mBuilder.getAdapter().getListData().size());
        mBuilder.getAdapter().notifyDataSetChanged();
    }

    private void onHandlerErrorView(FootRefreshRecyclerviewBinding binding) {
        if (mBuilder.getErrorImageRes() == 0) {
            binding.pageError.ivErrorImage.setImageResource(R.drawable.ic_page_net_error);
        } else {
            binding.pageError.ivErrorImage.setImageResource(mBuilder.getErrorImageRes());
        }
        if (!TextUtils.isEmpty(mBuilder.retryMsg)) {
            binding.pageError.tvErrorMsg.setText(mBuilder.retryMsg);
        }else {
            binding.pageError.tvErrorMsg.setText(R.string.network_error);
        }
    }

    private void addFootRefreshView() {
        if (footLoadMoreBean != null) return;
        footLoadMoreBean = new FootRefreshBean();
        footLoadMoreBean.setStatus(0);

        mBuilder.getAdapter().addSingleFootConfig(Constant.ADAPTER_FOOT_KEY.REFRESH_RECYCLER_FOOT_KEY, R.layout.foot_refresh_recyclerview, footLoadMoreBean);

        mBuilder.getAdapter().setFootDecorator(new BaseDataBindingDecorator<Object, ViewDataBinding>() {
            @Override
            public void decorator(ViewDataBinding holder, int position, int viewType, Object mData) {
                if (viewType == Constant.ADAPTER_FOOT_KEY.REFRESH_RECYCLER_FOOT_KEY) {

                    FootRefreshRecyclerviewBinding binding = (FootRefreshRecyclerviewBinding) holder;
                    onHandlerEmptyView(mBuilder.getEmptyMsg(), binding);
                    onHandlerErrorView(binding);
                    binding.pageError.pageError.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mPageErrorRetryListener != null)
                                mPageErrorRetryListener.errorRetry();
                        }
                    });
                    binding.pageEmpty.pageEmpty.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (mPageErrorRetryListener != null)
                                mPageErrorRetryListener.errorRetry();
                        }
                    });
                    if (mBuilder.isShowPageLayoutTop()) {
                        setLayoutParms(binding.pageLoading.llContent);
                        setLayoutParms(binding.pageEmpty.llContent);
                        setLayoutParms(binding.pageError.llContent);

                    }

                }

            }
        });

    }

    private void setLayoutParms(ViewGroup viewGroup) {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
        params.addRule(RelativeLayout.CENTER_HORIZONTAL);
        params.topMargin= (int) DisplayUtils.getDimension(R.dimen.dp_50);
        viewGroup.setLayoutParams(params);
    }

}
